home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / kode ƒ / kant build lists.c < prev    next >
C/C++ Source or Header  |  1995-02-10  |  12KB  |  505 lines

  1. #include "kant build lists.h"
  2. #include "list layer.h"
  3. #include "window layer.h"
  4. #include "text twiddling.h"
  5. #include "resource utilities.h"
  6. #include "util.h"
  7. #include <Icons.h>
  8.  
  9. enum { kReferenceList=0, kInstantList };
  10.  
  11. #define kReferenceListLineHeight    19
  12. #define kInstantListLineHeight        14
  13.  
  14. #define kInterestingIconID            650
  15. #define kReferenceLDEF                128
  16. #define kInstantLDEF                129
  17. #define kGrowBoxSize                15
  18.  
  19. static    short            gWhichListActive;
  20. static    ListHandle        gReferenceListHandle=0L;
  21. static    ListHandle        gInstantListHandle=0L;
  22. static    Rect            gReferenceListRect;
  23. static    Rect            gInstantListRect;
  24. static    short            gMaxRefsToDisplay;
  25. static    Handle            gInterestingIconHandle=0L;
  26.  
  27. void InitTheBuildLists(void)
  28. /* don't call this more than once */
  29. {
  30.     GetIconSuite(&gInterestingIconHandle, kInterestingIconID, svAllSmallData);
  31.     gReferenceListHandle=gInstantListHandle=0L;
  32.     gWhichListActive=kReferenceList;
  33. }
  34.  
  35. ListHandle GetReferenceListHandle(void)
  36. {
  37.     return gReferenceListHandle;
  38. }
  39.  
  40. ListHandle GetInstantListHandle(void)
  41. {
  42.     return gInstantListHandle;
  43. }
  44.  
  45. void GetReferenceListRect(Rect *theRect)
  46. {
  47.     *theRect=gReferenceListRect;
  48. }
  49.  
  50. void GetInstantListRect(Rect *theRect)
  51. {
  52.     *theRect=gInstantListRect;
  53. }
  54.  
  55. Boolean ReferenceListActiveQQ(void)
  56. {
  57.     return (gWhichListActive==kReferenceList);
  58. }
  59.  
  60. Boolean InstantListActiveQQ(void)
  61. {
  62.     return (gWhichListActive==kInstantList);
  63. }
  64.  
  65. Boolean HighlightedReferenceInterestingQQ(void)
  66. {
  67.     Cell            theCell;
  68.     Str255            itemStr;
  69.     Handle            iconHandle;
  70.     
  71.     if (MyGetFirstSelectedCell(gReferenceListHandle, &theCell))
  72.     {
  73.         MyGetCellData(gReferenceListHandle, theCell, itemStr);
  74.         Mymemcpy((Ptr)&iconHandle, (Ptr)&itemStr[1], 4);
  75.         return (iconHandle!=0L);
  76.     }
  77.     
  78.     return FALSE;
  79. }
  80.  
  81. Boolean SetHighlightedReferenceInteresting(Boolean isInteresting)
  82. {
  83.     Cell            theCell;
  84.     Str255            itemStr;
  85.     
  86.     if (MyGetFirstSelectedCell(gReferenceListHandle, &theCell))
  87.     {
  88.         MyGetCellData(gReferenceListHandle, theCell, itemStr);
  89.         if (isInteresting)
  90.         {
  91.             Mymemcpy((Ptr)&itemStr[1], (Ptr)&gInterestingIconHandle, 4);
  92.         }
  93.         else
  94.         {
  95.             Mymemset((Ptr)&itemStr[1], 0, 4);
  96.         }
  97.         MySetCellData(gReferenceListHandle, theCell, itemStr);
  98.     }
  99.     
  100.     return FALSE;
  101. }
  102.  
  103. void SetReferenceListActive(Boolean redraw)
  104. {
  105.     if (gWhichListActive!=kReferenceList)
  106.     {
  107.         gWhichListActive=kReferenceList;
  108.         if (redraw)
  109.         {
  110.             UpdateBuildLists((**gReferenceListHandle).port);
  111.         }
  112.     }
  113. }
  114.  
  115. void SetInstantListActive(Boolean redraw)
  116. {
  117.     if (gWhichListActive!=kInstantList)
  118.     {
  119.         gWhichListActive=kInstantList;
  120.         if (redraw)
  121.         {
  122.             UpdateBuildLists((**gInstantListHandle).port);
  123.         }
  124.     }
  125. }
  126.  
  127. void SetMaxReferenceDisplay(short numRefs, Boolean redraw)
  128. {
  129.     gMaxRefsToDisplay=numRefs;
  130.     if (redraw)
  131.     {
  132. // more stuff here to resize and display
  133.     }
  134. }
  135.  
  136. void SetHighlightedReference(short refNum)
  137. {
  138.     Cell            theCell;
  139.     
  140.     SetPt(&theCell, 0, refNum);
  141.     MySelectOneCell(gReferenceListHandle, theCell);
  142. }
  143.  
  144. void SetHighlightedInstant(short instantNum)
  145. {
  146.     Cell            theCell;
  147.     
  148.     SetPt(&theCell, 0, instantNum);
  149.     MySelectOneCell(gInstantListHandle, theCell);
  150. }
  151.  
  152. short GetNumberOfReferences(void)
  153. {
  154.     return (**gReferenceListHandle).dataBounds.bottom;
  155. }
  156.  
  157. short GetNumberOfInstants(void)
  158. {
  159.     return (**gInstantListHandle).dataBounds.bottom;
  160. }
  161.  
  162. short GetNumberOfVisibleReferences(void)
  163. {
  164.     return (**gReferenceListHandle).visible.bottom-(**gReferenceListHandle).visible.top;
  165. }
  166.  
  167. short GetNumberOfVisibleInstants(void)
  168. {
  169.     return (**gInstantListHandle).visible.bottom-(**gInstantListHandle).visible.top;
  170. }
  171.  
  172. short GetHighlightedReference(void)
  173. {
  174.     Cell            theCell;
  175.     
  176.     if (MyGetFirstSelectedCell(gReferenceListHandle, &theCell))
  177.         return theCell.v;
  178.     else
  179.         return -1;
  180. }
  181.  
  182. short GetHighlightedInstant(void)
  183. {
  184.     Cell            theCell;
  185.     
  186.     if (MyGetFirstSelectedCell(gInstantListHandle, &theCell))
  187.         return theCell.v;
  188.     else
  189.         return -1;
  190. }
  191.  
  192. void GetHighlightedReferenceName(Str255 theStr)
  193. {
  194.     Cell            theCell;
  195.     Str255            itemStr;
  196.     
  197.     if (MyGetFirstSelectedCell(gReferenceListHandle, &theCell))
  198.     {
  199.         MyGetCellData(gReferenceListHandle, theCell, itemStr);
  200.         Mymemcpy((Ptr)&theStr[1], (Ptr)&itemStr[5], itemStr[5]-4);
  201.         theStr[0]=itemStr[0]-4;
  202.     }
  203.     else theStr[0]=0x00;
  204. }
  205.  
  206. void GetHighlightedInstantName(Str255 theStr)
  207. {
  208.     Cell            theCell;
  209.     
  210.     if (MyGetFirstSelectedCell(gInstantListHandle, &theCell))
  211.     {
  212.         MyGetCellData(gInstantListHandle, theCell, theStr);
  213.     }
  214.     else theStr[0]=0x00;
  215. }
  216.  
  217. Boolean DeleteHighlightedReference(void)
  218. {
  219.     Cell            theCell;
  220.     
  221.     if (MyGetFirstSelectedCell(gReferenceListHandle, &theCell))
  222.     {
  223.         MyDeleteItemFromList(gReferenceListHandle, theCell.v);
  224.         BuildInstantListHandle(GetWindowFS((**gReferenceListHandle).port), TRUE);
  225.         return TRUE;
  226.     }
  227.     
  228.     return FALSE;
  229. }
  230.  
  231. Boolean DeleteHighlightedInstantiation(void)
  232. {
  233.     Cell            theCell;
  234.     
  235.     if (MyGetFirstSelectedCell(gInstantListHandle, &theCell))
  236.     {
  237.         MyDeleteItemFromList(gInstantListHandle, theCell.v);
  238.         return TRUE;
  239.     }
  240.     
  241.     return FALSE;
  242. }
  243.  
  244. void AddReferenceToList(Str255 theStr, Boolean isInteresting, Boolean autoSelect)
  245. {
  246.     Str255            itemStr;
  247.     Cell            theCell;
  248.     
  249.     itemStr[0]=0x04;
  250.     if (isInteresting)
  251.     {
  252.         Mymemcpy((Ptr)&itemStr[1], (Ptr)&gInterestingIconHandle, 4);
  253.     }
  254.     else
  255.     {
  256.         Mymemset((Ptr)&itemStr[1], 0, 4);
  257.     }
  258.     Mymemcpy((Ptr)&itemStr[5], (Ptr)&theStr[1], theStr[0]);
  259.     itemStr[0]+=theStr[0];
  260.     if ((!ReferenceListActiveQQ()) && autoSelect)
  261.         SetReferenceListActive(TRUE);
  262.     
  263.     MyAddStr255ToList(gReferenceListHandle, itemStr);
  264.     if (autoSelect)
  265.     {
  266.         SetPt(&theCell, 0, GetNumberOfReferences()-1);
  267.         MySelectOneCell(gReferenceListHandle, theCell);
  268.         BuildInstantListHandle(GetWindowFS((**gReferenceListHandle).port), TRUE);
  269.     }
  270. }
  271.  
  272. void AddInstantToList(Str255 theStr, Boolean autoSelect)
  273. {
  274.     Cell            theCell;
  275.     
  276.     if ((!InstantListActiveQQ()) && autoSelect)
  277.         SetInstantListActive(TRUE);
  278.     
  279.     MyAddStr255ToList(gInstantListHandle, theStr);
  280.     if (autoSelect)
  281.     {
  282.         SetPt(&theCell, 0, GetNumberOfInstants()-1);
  283.         MySelectOneCell(gInstantListHandle, theCell);
  284.     }
  285. }
  286.  
  287. void EditReferenceInList(short index, Str255 newStr, Boolean isInteresting)
  288. /* index is 0-based */
  289. {
  290.     Str255            itemStr;
  291.     Cell            theCell;
  292.     
  293.     itemStr[0]=0x04;
  294.     if (isInteresting)
  295.     {
  296.         Mymemcpy((Ptr)&itemStr[1], (Ptr)&gInterestingIconHandle, 4);
  297.     }
  298.     else
  299.     {
  300.         Mymemset((Ptr)&itemStr[1], 0, 4);
  301.     }
  302.     Mymemcpy((Ptr)&itemStr[5], (Ptr)&newStr[1], newStr[0]);
  303.     itemStr[0]+=newStr[0];
  304.     SetPt(&theCell, 0, index);
  305.     MySetCellData(gReferenceListHandle, theCell, itemStr);
  306. }
  307.  
  308. void EditInstantInList(short index, Str255 newStr)
  309. /* index is 0-based */
  310. {
  311.     Cell            theCell;
  312.     
  313.     SetPt(&theCell, 0, index);
  314.     MySetCellData(gInstantListHandle, theCell, newStr);
  315. }
  316.  
  317. void SetBuildListRects(WindowPtr theWindow, short hSpace, short vSpace, short headerHeight)
  318. {
  319.     short            refBottom;
  320.     
  321.     SetPort(theWindow);
  322.     refBottom=kReferenceListLineHeight*gMaxRefsToDisplay+headerHeight+vSpace;
  323.     SetRect(&gReferenceListRect, hSpace, headerHeight+vSpace, GetWindowWidth(theWindow)-hSpace,
  324.         refBottom);
  325.     SetRect(&gInstantListRect, hSpace, refBottom+vSpace,
  326.         GetWindowWidth(theWindow)-hSpace, GetWindowHeight(theWindow)-kGrowBoxSize-vSpace);
  327. }
  328.  
  329. void CreateBuildLists(WindowPtr theWindow)
  330. {
  331.     gReferenceListHandle=MyCreateVerticalScrollingList(theWindow, gReferenceListRect, 1,
  332.         kReferenceLDEF, kReferenceListLineHeight);
  333.     MySetListSelectionFlags(gReferenceListHandle, lOnlyOne);
  334.     gInstantListHandle=MyCreateVerticalScrollingList(theWindow, gInstantListRect, 1,
  335.         kInstantLDEF, kInstantListLineHeight);
  336.     MySetListSelectionFlags(gInstantListHandle, lOnlyOne);
  337. }
  338.  
  339. OSErr BuildReferenceListHandle(FSSpec theFS, Boolean redraw)
  340. {
  341.     OSErr            oe;
  342.     short            oldRefNum, refNum, numStringResources, resID, i;
  343.     Boolean            alreadyOpen;
  344.     Str255            resName;
  345.     ResType            resType;
  346.     Handle            resHandle;
  347.     short            resAttrs;
  348.     
  349.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, TRUE))!=noErr)
  350.         return oe;
  351.     
  352.     LSetDrawingMode(FALSE, gReferenceListHandle);
  353.     MyClearAllCells(gReferenceListHandle);
  354.     
  355.     numStringResources=Count1Resources('STR#');
  356.     for (i=1; i<=numStringResources; i++)
  357.     {
  358.         resHandle=Get1IndResource('STR#', i);
  359.         if (resHandle==0L)
  360.         {
  361.             LSetDrawingMode(TRUE, gReferenceListHandle);
  362.             return ResError();
  363.         }
  364.         
  365.         GetResInfo(resHandle, &resID, &resType, resName);
  366.         oe=ResError();
  367.         if (oe!=noErr)
  368.         {
  369.             ReleaseResource(resHandle);
  370.             CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  371.             LSetDrawingMode(TRUE, gReferenceListHandle);
  372.             return oe;
  373.         }
  374.         resAttrs=GetResAttrs(resHandle);
  375.         oe=ResError();
  376.         ReleaseResource(resHandle);
  377.         if (oe!=noErr)
  378.         {
  379.             CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  380.             LSetDrawingMode(TRUE, gReferenceListHandle);
  381.             return oe;
  382.         }
  383.         
  384.         AddReferenceToList(resName, (resAttrs&0x00ff) & resLocked, FALSE);
  385.     }
  386.     
  387.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  388.     
  389.     LSetDrawingMode(TRUE, gReferenceListHandle);
  390.     
  391.     if (redraw)
  392.         MyUpdateList(gReferenceListHandle);
  393.     
  394.     return noErr;
  395. }
  396.  
  397. OSErr BuildInstantListHandle(FSSpec theFS, Boolean redraw)
  398. {
  399.     OSErr            oe;
  400.     short            oldRefNum, refNum, numStringsInResource, resID, j;
  401.     Boolean            alreadyOpen;
  402.     Str255            resName, theName;
  403.     ResType            resType;
  404.     Handle            resHandle;
  405.     Str255            refName;
  406.     Rect            eraseRect;
  407.     
  408.     if (GetHighlightedReference()<0)
  409.     {
  410.         EraseRect(&gInstantListRect);
  411.         return noErr;
  412.     }
  413.     
  414.     GetHighlightedReferenceName(refName);
  415.     
  416.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, TRUE))!=noErr)
  417.         return oe;
  418.     
  419.     resHandle=Get1NamedResource('STR#', refName);
  420.     if (resHandle==0L)
  421.         return ResError();
  422.     
  423.     LSetDrawingMode(FALSE, gInstantListHandle);
  424.     MyClearAllCells(gInstantListHandle);
  425.     
  426.     GetResInfo(resHandle, &resID, &resType, resName);
  427.     
  428.     numStringsInResource=**((short**)resHandle);
  429.     for (j=1; j<=numStringsInResource; j++)
  430.     {
  431. //        SpinCursor(0);
  432.         GetIndString(theName, resID, j);
  433.         AddInstantToList(theName, FALSE);
  434.     }
  435.     
  436. //    SetCursor(&qd.arrow);
  437.     ReleaseResource(resHandle);
  438.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  439.     
  440.     LSetDrawingMode(TRUE, gInstantListHandle);
  441.     
  442.     if (redraw)
  443.     {
  444.         eraseRect=gInstantListRect;
  445.         eraseRect.right-=16;
  446.         EraseRect(&eraseRect);
  447.         MyUpdateList(gInstantListHandle);
  448.     }
  449.     
  450.     return noErr;
  451. }
  452.  
  453. void UpdateBuildLists(WindowPtr theWindow)
  454. {
  455.     LUpdate(theWindow->visRgn, gReferenceListHandle);
  456.     MyDrawListBorder(gReferenceListHandle);
  457.     MyDrawActiveListBorder(gReferenceListHandle, ReferenceListActiveQQ());
  458.     LUpdate(theWindow->visRgn, gInstantListHandle);
  459.     MyDrawListBorder(gInstantListHandle);
  460.     MyDrawActiveListBorder(gInstantListHandle, InstantListActiveQQ());
  461. }
  462.  
  463. void ResizeBuildLists(WindowPtr theWindow, short hSpace, short vSpace, short headerHeight)
  464. {
  465.     short            refBottom;
  466.     
  467.     LSetDrawingMode(FALSE, gReferenceListHandle);
  468.     LSetDrawingMode(FALSE, gInstantListHandle);
  469.     refBottom=kReferenceListLineHeight*gMaxRefsToDisplay+headerHeight+vSpace;
  470.     SetRect(&gReferenceListRect, hSpace, headerHeight+vSpace, GetWindowWidth(theWindow)-hSpace,
  471.         refBottom);
  472.     SetRect(&gInstantListRect, hSpace, refBottom+vSpace,
  473.         GetWindowWidth(theWindow)-hSpace, GetWindowHeight(theWindow)-kGrowBoxSize-vSpace);
  474.     MySetListRect(gReferenceListHandle, gReferenceListRect);
  475.     MySetListRect(gInstantListHandle, gInstantListRect);
  476.     MySetListSize(gReferenceListHandle, gReferenceListRect.right-gReferenceListRect.left-16,
  477.         gReferenceListRect.bottom-gReferenceListRect.top, kReferenceListLineHeight);
  478.     MySetListSize(gInstantListHandle, gInstantListRect.right-gInstantListRect.left-16,
  479.         gInstantListRect.bottom-gInstantListRect.top, kInstantListLineHeight);
  480.     MyDrawListBorder(gReferenceListHandle);
  481.     MyDrawListBorder(gInstantListHandle);
  482.     if (ReferenceListActiveQQ())
  483.         MyDrawActiveListBorder(gReferenceListHandle, TRUE);
  484.     if (InstantListActiveQQ())
  485.         MyDrawActiveListBorder(gInstantListHandle, TRUE);
  486.     LSetDrawingMode(TRUE, gReferenceListHandle);
  487.     LSetDrawingMode(TRUE, gInstantListHandle);
  488.     InvalRect(&(theWindow->portRect));
  489. }
  490.  
  491. void GetGrowSizeTheLists(Rect *sizeRect, short hSpace, short vSpace, short headerHeight)
  492. {
  493.     SetRect(sizeRect, 294, 48+kGrowBoxSize+1+headerHeight+kReferenceListLineHeight*gMaxRefsToDisplay+vSpace*5,
  494.         32766, 32767);
  495. }
  496.  
  497. void DisposeBuildLists(void)
  498. {
  499.     if (gReferenceListHandle!=0L)
  500.         LDispose(gReferenceListHandle);
  501.     if (gInstantListHandle!=0L)
  502.         LDispose(gInstantListHandle);
  503.     gReferenceListHandle=gInstantListHandle=0L;
  504. }
  505.